/**
 * Created by lihaoyu on 2017/8/12.
 */
$(document).ready(function () {
    var users_data;
    $.ajax({
        type: "POST",
        url: "/users_data",
        dataType:'json',
        data: "",
        success:function(msg){
            users_data = msg;
            for(var count=0;count<msg.length;count++){
                $("#users_data").html(function(i,origText){
                    return origText+msg[count].id+'<input type="text" id="'+count+'pw" placeholder="new password"> <button class="submit" id="'+count+'sub">submit</button><button class="cancel" id="'+count+'cancel">delete</button>  <br>';
                });
            }
        },
        error:function(msg){
            console.log("error");
        }
    });

    $("body").on('click','.cancel',function(){
        $.ajax({
            type: "POST",
            url: "/root_cancel",
            dataType:'json',
            data: "ID="+users_data[Number($(this).attr("id").replace("cancel",""))].id,
            success:function (msg) {
                location.reload();
            }
        })
    });

    $("body").on('click','.submit',function(){
        var temp = Number($(this).attr("id").replace("sub",""));
        var pwMd5 = $.md5($("#"+temp+"pw").val());
        $.ajax({
            type: "POST",
            url: "/root_submit",
            dataType:'json',
            data: "ID="+users_data[temp].id+"&PW="+pwMd5,
            success:function (msg) {
                location.reload();
            }
        })
    });
});